install.packages('https://cran.r-project.org/src/contrib/Archive/interflex/interflex_1.0.8.tar.gz', 
                 repos=NULL, 
                 type='source')


library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(interflex)
library(gridExtra)
library(readxl)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


paste0(home, 'Code/interflex-kernel-spillover.R') %>%
  source
paste0(home, 'Code/randomization-inference-marginal-effects.R') %>%
  source


permutation.results = paste0(home, 'Data/RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'Data/experimental-analyses.Rds') %>%
  readRDS


dv_pooled = paste0(home, 'Data/pooled-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment))
class(dv_pooled) = 'data.frame'


tmp = ggplot(data=dv_pooled, 
             aes(x=Prop.Citizen, fill=factor(Citizen))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
citizen.pooled.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>% 
  mutate(xmin=0.974*(xmin+abs(min(xmin))), 
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)


tmp = ggplot(data=dv_pooled, 
             aes(x=Prop.Firm, fill=factor(Firm))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
firm.pooled.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>%
  mutate(xmin=0.974*(xmin+abs(min(xmin))),
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)
rm(tmp)


fig_7_2_left = rimer(result='tab3col3', 
                     X='Citizen', 
                     Z='Prop.Citizen')
fig_7_2_right = rimer(result='tab3col3', 
                      X='Firm', 
                      Z='Prop.Firm')
fig_7_2 = grid.arrange(fig_7_2_left, fig_7_2_right, 
                       nrow=1)


ggsave(filename='figure-A07-2.png', 
       plot=fig_7_2, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=3.75, 
       units='in')
ggsave(filename='figure-A07-2.eps', 
       plot=fig_7_2, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=3.75, 
       units='in', 
       device=cairo_ps)


